home *** CD-ROM | disk | FTP | other *** search
/ MaxiMac 2001 July / MaxiMac 116.iso / Macworld on CD n°116 / Mac OS X / Internet / Utilisation / Fizilla 5⁄30 / Components / nsProxyAutoConfig.js < prev    next >
Encoding:
Text File  |  2001-05-09  |  13.0 KB  |  404 lines  |  [TEXT/CWIE]

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *   Akhil Arora <akhil.arora@sun.com>
  22.  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
  23.  */
  24.  
  25. /*
  26.    Script for Proxy Auto Config in the new world order.
  27.        - Gagan Saksena 04/24/00 
  28. */
  29.  
  30. const kPAC_CONTRACTID = "@mozilla.org/network/proxy_autoconfig;1";
  31. const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
  32. const kDNS_CONTRACTID = "@mozilla.org/network/dns-service;1";
  33. const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
  34. const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
  35. const nsIIOService = Components.interfaces['nsIIOService'];
  36. const nsIDNSService = Components.interfaces.nsIDNSService;
  37.  
  38. function debug(msg)
  39. {
  40.     dump("\nPAC:" + msg + "\n\n");
  41. }
  42.  
  43. // implementor of nsIProxyAutoConfig
  44. function nsProxyAutoConfig() {};
  45.  
  46. // global variable that will hold the downloaded js 
  47. var pac = null;
  48. //hold PAC's URL, used in evalAsCodebase()
  49. var pacURL;
  50. // ptr to eval'ed FindProxyForURL function
  51. var LocalFindProxyForURL=null;
  52. // sendbox in which we eval loaded autoconfig js file
  53. var ProxySandBox = null;
  54.  
  55. nsProxyAutoConfig.prototype = {
  56.     sis: null,
  57.     done: false,
  58.  
  59.     ProxyForURL: function(url, host, port, type) {
  60.         /* If we're not done loading the pac yet, wait (ideally). For
  61.            now, just return DIRECT to avoid loops. A simple mutex
  62.            between ProxyForURL and LoadPACFromURL locks-up the
  63.            browser. */
  64.         if (!this.done) {
  65.             host.value = null;
  66.             type.value = "direct";
  67.             return;
  68.         }
  69.  
  70.         var uri = url.QueryInterface(Components.interfaces.nsIURI);
  71.         // Call the original function-
  72.         var proxy = LocalFindProxyForURL(uri.spec, uri.host);
  73.         debug("Proxy = " + proxy);
  74.         if (proxy == "DIRECT") {
  75.             host.value = null;
  76.             type.value = "direct";
  77.         }
  78.         else {
  79.             // TODO warn about SOCKS
  80.  
  81.             // we ignore everything else past the first proxy. 
  82.             // we could theoretically check isResolvable now and continue 
  83.             // parsing. but for now...
  84.             var hostport = /^PROXY ([^:]+):(\d+)/(proxy);
  85.             host.value = hostport[1];
  86.             port.value = hostport[2];
  87.             type.value = "http"; //proxy (http, socks, direct, etc)
  88.         }
  89.     },
  90.  
  91.     LoadPACFromURL: function(uri, ioService) {
  92.         debug("Loading PAC from " + uri.spec);
  93.         this.done = false;
  94.         var channel = ioService.newChannelFromURI(uri);
  95.         pacURL = uri.spec;
  96.         channel.asyncOpen(this, null);
  97.     },
  98.  
  99.     // nsIStreamListener interface
  100.     onStartRequest: function(request, ctxt) { 
  101.         pac = '';
  102.         LocalFindProxyForURL=null;
  103.         this.sis = 
  104.         Components.Constructor('@mozilla.org/scriptableinputstream;1',
  105.                                'nsIScriptableInputStream', 
  106.                                'init');
  107.     },
  108.  
  109.     onStopRequest: function(request, ctxt, status, errorMsg) {
  110.         this.done = true;
  111.         if(!ProxySandBox) {
  112.            ProxySandBox = new Sandbox();
  113.         }
  114.         // add predefined functions to pac
  115.         var mypac = pacUtils + pac;
  116.         // evaluate loded js file
  117.         evalInSandbox(mypac, ProxySandBox, pacURL);
  118.         ProxySandBox.myIP = dns.myIPAddress;
  119.         LocalFindProxyForURL=ProxySandBox.FindProxyForURL;
  120.     },
  121.  
  122.     onDataAvailable: function(request, ctxt, inStream, sourceOffset, count) {
  123.         var ins = new this.sis(inStream);
  124.         pac += ins.read(count);
  125.     }
  126. }
  127.  
  128. var pacModule = new Object();
  129.  
  130. pacModule.registerSelf =
  131.     function (compMgr, fileSpec, location, type) {
  132.         dump("*** Registering Proxy Auto Config (a Javascript module!) \n");
  133.         compMgr.registerComponentWithType(kPAC_CID,
  134.             "Proxy Auto Config",
  135.             kPAC_CONTRACTID,
  136.             fileSpec, location, 
  137.             true, true, type);
  138.     }
  139.  
  140. pacModule.getClassObject =
  141. function (compMgr, cid, iid) {
  142.         if (!cid.equals(kPAC_CID))
  143.             throw Components.results.NS_ERROR_NO_INTERFACE;
  144.  
  145.         if (!iid.equals(Components.interfaces.nsIFactory))
  146.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  147.  
  148.         return pacFactory;
  149.     }
  150.  
  151. pacModule.canUnload =
  152.     function (compMgr) {
  153.         dump("*** Unloading Proxy Auto Config...\n");
  154.         return true;
  155.     }
  156.  
  157. var pacFactory = new Object();
  158. pacFactory.createInstance =
  159.     function (outer, iid) {
  160.         if (outer != null)
  161.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  162.  
  163.         if (!iid.equals(nsIProxyAutoConfig) &&
  164.             !!iid.equals(Components.interfaces.nsIStreamListener) &&
  165.             !iid.equals(Components.interfaces.nsISupports)) {
  166.             // shouldn't this be NO_INTERFACE?
  167.             throw Components.results.NS_ERROR_INVALID_ARG;
  168.         }
  169.         return PacMan;
  170.     }
  171.  
  172. function NSGetModule(compMgr, fileSpec) {
  173.     return pacModule;
  174. }
  175.  
  176. var PacMan = new nsProxyAutoConfig() ;
  177.  
  178. var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
  179. var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
  180.  
  181. var pacUtils = 
  182. "function dnsDomainIs(host, domain) {\n" +
  183. "    return (host.length >= domain.length &&\n" +
  184. "            host.substring(host.length - domain.length) == domain);\n" +
  185. "}\n" +
  186.  
  187. "function dnsDomainLevels(host) {\n" +
  188. "    return host.split('.').length-1;\n" +
  189. "}\n" +
  190.  
  191. "function convert_addr(ipchars) {\n"+
  192. "    var bytes = ipchars.split('.');\n"+
  193. "    var result = ((bytes[0] & 0xff) << 24) |\n"+
  194. "                 ((bytes[1] & 0xff) << 16) |\n"+
  195. "                 ((bytes[2] & 0xff) <<  8) |\n"+
  196. "                  (bytes[3] & 0xff);\n"+
  197. "    return result;\n"+
  198. "}\n"+
  199.  
  200. "function isInNet(ipaddr, pattern, maskstr) {\n"+
  201. "    var test = /^(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})$/(ipaddr);\n"+
  202. "    if (test == null) {\n"+
  203. "        ipaddr = dnsResolve(ipaddr);\n"+
  204. "    } else if (test[1] > 255 || test[2] > 255 || \n"+
  205. "               test[3] > 255 || test[4] > 255) {\n"+
  206. "        return false;    // not an IP address\n"+
  207. "    }\n"+
  208. "    var host = convert_addr(ipaddr);\n"+
  209. "    var pat  = convert_addr(pattern);\n"+
  210. "    var mask = convert_addr(maskstr);\n"+
  211. "    return ((host & mask) == (pat & mask));\n"+
  212. "    \n"+
  213. "}\n"+
  214.  
  215. "function isPlainHostName(host) {\n" +
  216. "    return (host.search('\\.') == -1);\n" +
  217. "}\n" +
  218.  
  219. "function isResolvable(host) {\n" +
  220. "    var ip = dnsResolve(host);\n" +
  221. "    return (ip != null);\n" +
  222. "}\n" +
  223.  
  224. "function localHostOrDomainIs(host, hostdom) {\n" +
  225. "    if (isPlainHostName(host)) {\n" +
  226. "        return (hostdom.search('/^' + host + '/') != -1);\n" +
  227. "    }\n" +
  228. "    else {\n" +
  229. "        return (host == hostdom); //TODO check \n" +
  230. "    }\n" +
  231. "}\n" +
  232.  
  233. " var myIP;\n" +
  234. "function myIpAddress() {\n" +
  235. "    return (myIP) ? myIP : '127.0.0.1';\n" +
  236. "}\n" +
  237.  
  238. "function shExpMatch(url, pattern) {\n" +
  239. "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
  240. "   pattern = pattern.replace(/\\*/g, '.*');\n" +
  241. "   pattern = pattern.replace(/\\?/g, '.');\n" +
  242. "   var newRe = new RegExp(pattern);\n" +
  243. "   return newRe.test(url);\n" +
  244. "}\n" +
  245.  
  246. "var wdays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT');\n" +
  247.  
  248. "var monthes = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');\n"+
  249.  
  250. "function weekdayRange() {\n" +
  251. "    function getDay(weekday) {\n" +
  252. "        for (var i = 0; i < 6; i++) {\n" +
  253. "            if (weekday == wdays[i]) \n" +
  254. "                return i;\n" +
  255. "        }\n" +
  256. "        return -1;\n" +
  257. "    }\n" +
  258. "    var date = new Date();\n" +
  259. "    var argc = arguments.length;\n" +
  260. "    var wday;\n" +
  261. "    if (argc < 1)\n" +
  262. "        return false;\n" +
  263. "    if (arguments[argc - 1] == 'GMT') {\n" +
  264. "        argc--;\n" +
  265. "        wday = date.getUTCDay();\n" +
  266. "    } else {\n" +
  267. "        wday = date.getDay();\n" +
  268. "    }\n" +
  269. "    var wd1 = getDay(arguments[0]);\n" +
  270. "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
  271. "    return (wd1 == -1 || wd2 == -1) ? false\n" +
  272. "                                    : (wd1 <= wday && wday <= wd2);\n" +
  273. "}\n" +
  274.  
  275. "function dateRange() {\n" +
  276. "    function getMonth(name) {\n" +
  277. "        for (var i = 0; i < 6; i++) {\n" +
  278. "            if (name == monthes[i])\n" +
  279. "                return i;\n" +
  280. "        }\n" +
  281. "        return -1;\n" +
  282. "    }\n" +
  283. "    var date = new Date();\n" +
  284. "    var argc = arguments.length;\n" +
  285. "    if (argc < 1) {\n" +
  286. "        return false;\n" +
  287. "    }\n" +
  288. "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
  289. "\n" +
  290. "    if (isGMT) {\n" +
  291. "        argc--;\n" +
  292. "    }\n" +
  293. "    // function will work even without explict handling of this case\n" +
  294. "    if (argc == 1) {\n" +
  295. "        var tmp = parseInt(arguments[0]);\n" +
  296. "        if (isNaN(tmp)) {\n" +
  297. "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
  298. "getMonth(arguments[0]));\n" +
  299. "        } else if (tmp < 32) {\n" +
  300. "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
  301. "        } else { \n" +
  302. "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
  303. "tmp);\n" +
  304. "        }\n" +
  305. "    }\n" +
  306. "    var year = date.getFullYear();\n" +
  307. "    var date1, date2;\n" +
  308. "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
  309. "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
  310. "    var adjustMonth = false;\n" +
  311. "    for (var i = 0; i < (argc >> 1); i++) {\n" +
  312. "        var tmp = parseInt(arguments[i]);\n" +
  313. "        if (isNaN(tmp)) {\n" +
  314. "            var mon = getMonth(arguments[i]);\n" +
  315. "            date1.setMonth(mon);\n" +
  316. "        } else if (tmp < 32) {\n" +
  317. "            adjustMonth = (argc <= 2);\n" +
  318. "            date1.setDate(tmp);\n" +
  319. "        } else {\n" +
  320. "            date1.setFullYear(tmp);\n" +
  321. "        }\n" +
  322. "    }\n" +
  323. "    for (var i = (argc >> 1); i < argc; i++) {\n" +
  324. "        var tmp = parseInt(arguments[i]);\n" +
  325. "        if (isNaN(tmp)) {\n" +
  326. "            var mon = getMonth(arguments[i]);\n" +
  327. "            date2.setMonth(mon);\n" +
  328. "        } else if (tmp < 32) {\n" +
  329. "            date2.setDate(tmp);\n" +
  330. "        } else {\n" +
  331. "            date2.setFullYear(tmp);\n" +
  332. "        }\n" +
  333. "    }\n" +
  334. "    if (adjustMonth) {\n" +
  335. "        date1.setMonth(date.getMonth());\n" +
  336. "        date2.setMonth(date.getMonth());\n" +
  337. "    }\n" +
  338. "    if (isGMT) {\n" +
  339. "    var tmp = date;\n" +
  340. "        tmp.setFullYear(date.getUTCFullYear());\n" +
  341. "        tmp.setMonth(date.getUTCMonth());\n" +
  342. "        tmp.setDate(date.getUTCDate());\n" +
  343. "        tmp.setHours(date.getUTCHours());\n" +
  344. "        tmp.setMinutes(date.getUTCMinutes());\n" +
  345. "        tmp.setSeconds(date.getUTCSeconds());\n" +
  346. "        date = tmp;\n" +
  347. "    }\n" +
  348. "    return ((date1 <= date) && (date <= date2));\n" +
  349. "}\n" +
  350.  
  351. "function timeRange() {\n" +
  352. "    var argc = arguments.length;\n" +
  353. "    var date = new Date();\n" +
  354. "    var isGMT= false;\n"+
  355. "\n" +
  356. "    if (argc < 1) {\n" +
  357. "        return false;\n" +
  358. "    }\n" +
  359. "    if (arguments[argc - 1] == 'GMT') {\n" +
  360. "        isGMT = true;\n" +
  361. "        argc--;\n" +
  362. "    }\n" +
  363. "\n" +
  364. "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
  365. "    var date1, date2;\n" +
  366. "    date1 = new Date();\n" +
  367. "    date2 = new Date();\n" +
  368. "\n" +
  369. "    if (argc == 1) {\n" +
  370. "        return (hour == arguments[0]);\n" +
  371. "    } else if (argc == 2) {\n" +
  372. "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
  373. "    } else {\n" +
  374. "        switch (argc) {\n" +
  375. "        case 6:\n" +
  376. "            date1.setSeconds(arguments[2]);\n" +
  377. "            date2.setSeconds(arguments[5]);\n" +
  378. "        case 4:\n" +
  379. "            var middle = argc >> 1;\n" +
  380. "            date1.setHours(arguments[0]);\n" +
  381. "            date1.setMinutes(arguments[1]);\n" +
  382. "            date2.setHours(arguments[middle]);\n" +
  383. "            date2.setMinutes(arguments[middle + 1]);\n" +
  384. "            if (middle == 2) {\n" +
  385. "                date2.setSeconds(59);\n" +
  386. "            }\n" +
  387. "            break;\n" +
  388. "        default:\n" +
  389. "          throw 'timeRange: bad number of arguments'\n" +
  390. "        }\n" +
  391. "    }\n" +
  392. "\n" +
  393. "    if (isGMT) {\n" +
  394. "        date.setFullYear(date.getUTCFullYear());\n" +
  395. "        date.setMonth(date.getUTCMonth());\n" +
  396. "        date.setDate(date.getUTCDate());\n" +
  397. "        date.setHours(date.getUTCHours());\n" +
  398. "        date.setMinutes(date.getUTCMinutes());\n" +
  399. "        date.setSeconds(date.getUTCSeconds());\n" +
  400. "    }\n" +
  401. "    return ((date1 <= date) && (date <= date2));\n" +
  402. "}\n"
  403.  
  404.